<?php

namespace app\utils;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Email
{
    protected $mail;

    public function __construct()
    {
        $this->mail = new PHPMailer(true);
        // 邮件服务器配置
        $this->mail->isSMTP();
        $this->mail->Host = 'smtp.126.com';
        $this->mail->SMTPAuth = true;
        $this->mail->Username = 'ink279069@126.com'; // 你的邮箱
        $this->mail->Password = 'DWhcUGkuV3uzcfW3';
        $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $this->mail->Port = 465;
        $this->mail->CharSet = 'UTF-8';
    }

    public function sendVerificationCode($to, $code)
    {
        try {
            $this->mail->setFrom($this->mail->Username, '校园活动平台');
            $this->mail->addAddress($to);
            $this->mail->isHTML(true);
            $this->mail->Subject = '验证码';
            $this->mail->Body = "您的验证码是：{$code}，有效期为5分钟。";

            return $this->mail->send();
        } catch (Exception $e) {
            // 记录错误日志
            error("邮件发送失败: " . $e->getMessage());
            return false;
        }
    }
}
